const db = require('../db/index')

exports.articles=(req,res)=>{
    const sql = `select * from article_cate where is_delete=0 order by id asc`
    db.query(sql,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('获取文章失败')
        res.send({
            status:200,
            message:'获取文章成功',
            data:results
        })
    })
}

exports.addart=(req,res)=>{
    const sql = `insert into  article_cate set ?`
    db.query(sql,req.body,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('新增失败')
        res.send({
            status:200,
            message:'新增成功'
        })
    })
}

exports.deleteArt=(req,res)=>{
    const sql = `update article_cate set is_delete = 1 where id =?`
    db.query(sql,req.params.id,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('删除失败')
        res.send({
            status:200,
            message:'删除成功'
        })
    })
}


exports.serchArt=(req,res)=>{
    const sql = `select * from  article_cate  where name =?`
    db.query(sql,req.params.name,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('查询失败')
        res.send({
            status:200,
            message:'查询成功',
            data:results
        })
    })
}

exports.updateArt=(req,res)=>{
    const sql = `update article_cate set ? where id =?`
    db.query(sql,[req.body,req.body.id],(err,results)=>{
        if(err) return res.fail(err)
        if(results.affectedRows !== 1) return res.fail('更新失败')
        res.send({
            status:200,
            message:'更新成功',
        })
    })
}